package com.itheima.wallet.web.Controller.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import com.itheima.wallet.common.domain.User;
import com.itheima.wallet.common.dtos.UserDto;
import com.itheima.wallet.common.enums.AppHttpCodeEnum;
import com.itheima.wallet.common.vo.ResponseResult;
import com.itheima.wallet.user.api.UserService;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("user")
public class UserControllerImpl {


    @Reference
    private UserService userService;



    /**
     * 注册新用户
     *
     * @param userDto
     * @return
     */
    @PostMapping("register")
    public ResponseEntity<ResponseResult> register(UserDto userDto) {
        ResponseResult responseResult = null;
        //校验验证码
        String phone = userDto.getPhone();
        boolean checkResult = userService.checkSMSCode(phone, userDto.getCode(), "register");
        if (!checkResult) {
            //验证码不通过
            responseResult = ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseResult);
        }
        //验证码通过，封装User对象
        String userDtoPassword = userDto.getPassword();
        //加盐
        String password = phone + userDtoPassword;
        //雪花算法生成account_id
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        String account_id = snowflake.nextIdStr();

        User user = User.builder()
                .username(userDto.getUsername())
                .password(SecureUtil.md5(password))
                .phone(phone)
                .account_id(account_id)
                .build();

        responseResult = userService.register(user);
        if (responseResult.getCode() == 0) {
            return ResponseEntity.ok(responseResult);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseResult);
    }

    /**
     * 登录
     *
     * @param phone 手机号
     * @param code  验证码
     * @return
     */
    @PostMapping("login")
    public ResponseEntity<Object> login(String phone, String code) {
        ResponseResult responseResult = userService.login(phone, code);
        if (responseResult.getCode() == 0) {
            return ResponseEntity.ok(responseResult);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseResult);
    }

    /**
     * 登录发送动态验证码
     *
     * @param phone 手机号
     * @return
     */
    @GetMapping("login/sendCode")
    public ResponseEntity<ResponseResult> loginSendCode(String phone) {
        ResponseResult responseResult = userService.sendCode(phone, "login");
        if (responseResult.getCode() == 0) {
            return ResponseEntity.ok(responseResult);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseResult);
    }

    /**
     * 登录发送动态验证码
     *
     * @param phone 手机号
     * @return
     */
    @GetMapping("register/sendCode")
    public ResponseEntity<ResponseResult> registerSendCode(String phone) {
        ResponseResult responseResult = userService.sendCode(phone, "register");
        if (responseResult.getCode() == 0) {
            return ResponseEntity.ok(responseResult);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(responseResult);
    }
}
